package day21.colllection;

import java.util.Scanner;

/**
 * 学生管理交互式测试类，支持从键盘输入
 */
public class TestStudentInteractive {
    public static void main(String[] args) {
        // 创建学生管理对象
        StudentManager manager = new StudentManager();
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("======= 学生信息管理系统 =======");
        
        while (true) {
            // 显示菜单
            System.out.println("\n请选择操作：");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生");
            System.out.println("4. 查询学生（按学号）");
            System.out.println("5. 查询学生（按姓名）");
            System.out.println("6. 显示所有学生");
            System.out.println("7. 显示学生总数");
            System.out.println("0. 退出系统");
            System.out.print("请输入选项（0-7）：");
            
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符
            
            switch (choice) {
                case 1:
                    addStudentInteractive(manager, scanner);
                    break;
                case 2:
                    deleteStudentInteractive(manager, scanner);
                    break;
                case 3:
                    updateStudentInteractive(manager, scanner);
                    break;
                case 4:
                    findStudentByIdInteractive(manager, scanner);
                    break;
                case 5:
                    findStudentsByNameInteractive(manager, scanner);
                    break;
                case 6:
                    manager.displayAllStudents();
                    break;
                case 7:
                    System.out.println("当前学生总数：" + manager.getStudentCount());
                    break;
                case 0:
                    System.out.println("感谢使用学生信息管理系统！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
        }
    }
    
    /**
     * 交互式添加学生
     */
    private static void addStudentInteractive(StudentManager manager, Scanner scanner) {
        System.out.println("\n--- 添加学生 ---");
        System.out.print("请输入学号：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消费换行符
        
        System.out.print("请输入姓名：");
        String name = scanner.nextLine();
        
        System.out.print("请输入年龄：");
        int age = scanner.nextInt();
        scanner.nextLine(); // 消费换行符
        
        System.out.print("请输入专业：");
        String major = scanner.nextLine();
        
        manager.addStudent(new Student(id, name, age, major));
    }
    
    /**
     * 交互式删除学生
     */
    private static void deleteStudentInteractive(StudentManager manager, Scanner scanner) {
        System.out.println("\n--- 删除学生 ---");
        System.out.print("请输入要删除的学生学号：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消费换行符
        
        manager.deleteStudent(id);
    }
    
    /**
     * 交互式修改学生信息
     */
    private static void updateStudentInteractive(StudentManager manager, Scanner scanner) {
        System.out.println("\n--- 修改学生信息 ---");
        System.out.print("请输入要修改的学生学号：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消费换行符
        
        Student student = manager.findStudentById(id);
        if (student != null) {
            System.out.println("找到学生：" + student);
            System.out.print("请输入新姓名：");
            String newName = scanner.nextLine();
            
            System.out.print("请输入新年龄：");
            int newAge = scanner.nextInt();
            scanner.nextLine(); // 消费换行符
            
            System.out.print("请输入新专业：");
            String newMajor = scanner.nextLine();
            
            manager.updateStudent(id, newName, newAge, newMajor);
        } else {
            System.out.println("未找到学号为" + id + "的学生！");
        }
    }
    
    /**
     * 交互式按学号查询学生
     */
    private static void findStudentByIdInteractive(StudentManager manager, Scanner scanner) {
        System.out.println("\n--- 按学号查询学生 ---");
        System.out.print("请输入要查询的学生学号：");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消费换行符
        
        Student student = manager.findStudentById(id);
        if (student != null) {
            System.out.println("找到学生：" + student);
        } else {
            System.out.println("未找到学号为" + id + "的学生！");
        }
    }
    
    /**
     * 交互式按姓名查询学生
     */
    private static void findStudentsByNameInteractive(StudentManager manager, Scanner scanner) {
        System.out.println("\n--- 按姓名查询学生 ---");
        System.out.print("请输入要查询的学生姓名：");
        String name = scanner.nextLine();
        
        java.util.List<Student> students = manager.findStudentsByName(name);
        if (!students.isEmpty()) {
            System.out.println("找到 " + students.size() + " 个姓名为\"" + name + "\"的学生：");
            for (Student student : students) {
                System.out.println(student);
            }
        } else {
            System.out.println("未找到姓名为\"" + name + "\"的学生！");
        }
    }
}